﻿Imports System.Collections.Specialized
Imports System.Windows.Forms

Public Class BudgetAdd
    Public Overrides Function DoAddCheck() As Boolean
        DoAddCheck = False
        Me.ValidateChildren()

        If Me.cboSoftWareType.Text.Trim = String.Empty Then
            ShowMessageBox("支出预算类型不能为空")
            Me.btnSelectType.Focus()
            Exit Function
        End If

        If numUpDown.Value <= 0 Then
            ShowMessageBox("支出预算不能小于0")
            Me.numUpDown.Focus()
            Exit Function
        End If

        If Me.txtUsername.Text.Trim = String.Empty Then
            ShowMessageBox("支出预算用户不能为空")
            Me.btnUsername.Focus()
            Exit Function
        End If

        DoAddCheck = True
    End Function

    Public Overrides Function DoAddExist() As Boolean
        Return True
    End Function

    Public Sub ClearForm()
        Me.cboSoftWareType.Text = ""
        cboSoftWareType.Tag = ""
        Me.numUpDown.Value = 0
        'Me.dt1.Value = Now
        Me.txtMemo.Text = ""
    End Sub

    Private Sub btnSelectType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectType.Click
        Dim ttf As Wys.Control.Vb.TreeTableFromBase = New Wys.Control.Vb.TreeTableFromBase(Me.cboSoftWareType.Tag.ToString())
        ttf.ID = "ID"
        ttf.ParentID = "ParentID"
        ttf.DisplayName = "Name"
        ttf.TableName = "XscBugType"
        '根据配置设置窗体的界面显示内容
        ' 0 为代码窗口 1 为项目学习窗口 2 为BUG窗口 3 为英语文章  4 为英语文章
        ttf.Text = "选择支出预算类型"
        ttf.TypeId = 10
        ttf.ShowDialog()
        If (Not ttf.Cancel) Then
            cboSoftWareType.Text = ttf.DisplayNameValue
            cboSoftWareType.Tag = ttf.Result
        End If
        cboSoftWareType.Text = ct.GetParentName(cboSoftWareType.Tag.ToString(), "XscBugType", "Name", "ParentID", "ID")
    End Sub
    Public Overrides Sub InitAdd()
        ClearForm()
    End Sub

    Private Sub InputAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If cboSoftWareType Is Nothing Then
            cboSoftWareType.Tag = ""
        End If

    End Sub

    Public Overrides Function DoInsert() As Boolean
        MyBase.DoInsert()
        DoInsert = False

        Dim dt内容1 As DataTable = ct.FillTable("select * from budgetOut where TYPE= " & cboSoftWareType.Tag.ToString & " And UserName ='" & Me.txtUsername.Tag.ToString & "' AND YEAR=" & Me.dt1.Value.Year & " AND MONTH=" & Me.dt1.Value.Month)
        If dt内容1.Rows.Count > 0 Then
            ShowMessageBox("已经存在")
            Exit Function
        End If

        Dim cmd As New ADODB.Command
        Dim sb As New StringBuilder
        sb.AppendLine(" Insert Into ")
        sb.AppendLine(" budgetOut ")
        sb.AppendLine(" (")
        sb.AppendLine(" TYPE ")
        sb.AppendLine(" ,Count ")
        sb.AppendLine(" ,YEAR ")
        sb.AppendLine(" ,MONTH ")
        sb.AppendLine(" ,Memo ")
        sb.AppendLine(" ,UserName ")
        sb.AppendLine(" ,OPTIME ")
        sb.AppendLine(" ,id ")
        sb.AppendLine(" ) ")
        sb.AppendLine(" Values ")
        sb.AppendLine(" ( ")
        sb.AppendLine(" ? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ) ")
        Dim pa As ADODB.Parameter

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Value = Me.cboSoftWareType.Tag
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDecimal, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Value = numUpDown.Value
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 4, dt1.Value.Year.ToString)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 4, dt1.Value.Month.ToString)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 100, Me.txtMemo.Text.Trim)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 36, Me.txtUsername.Tag.ToString.Trim)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        Dim strLastId As String = System.Guid.NewGuid.ToString("D")
        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 36, strLastId)
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
        If ct.ExcCommand(cmd) Then
            '更新关联数据
            Me.bAdd = True '表示成功插入数据能刷新界面        
            ShowMessageBox("插入成功")
            ClearForm()
            DoInsert = True
        Else
            DoInsert = False
        End If
    End Function

#Region "更新模式"

    Public Overrides Function DoUpdate() As Boolean
        MyBase.DoUpdate()
        DoUpdate = False

        Dim sb As New StringBuilder
        Dim cmd As New ADODB.Command
        sb.Length = 0

        sb.AppendLine(" UPDATE ")
        sb.AppendLine(" budgetOut ")
        sb.AppendLine(" SET ")
        sb.AppendLine(" TYPE=? ")
        sb.AppendLine(" ,Count=? ")
        sb.AppendLine(" ,Year=? ")
        sb.AppendLine(" ,Month=? ")
        sb.AppendLine(" ,Memo=? ")
        sb.AppendLine(" ,UserName=? ")
        sb.AppendLine(" ,OPTIME=? ")
        sb.AppendLine("  WHERE  ")
        sb.AppendLine("  ID=? ")

        Dim pa As ADODB.Parameter

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Value = Me.cboSoftWareType.Tag
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDecimal, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Value = numUpDown.Value
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 4, dt1.Value.Year.ToString)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 4, dt1.Value.Month.ToString)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 100, Me.txtMemo.Text.Trim)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 36, Me.txtUsername.Tag.ToString.Trim)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 36, CStr(Me.参数列表("ID")))
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        If (ct.ExcCommand(cmd)) Then
            '更新关联数据
            ShowMessageBox("更新成功")
            bAdd = True
            Me.Close()
        Else
            ShowMessageBox("更新失败")
        End If
        DoUpdate = True
    End Function

    Public Overrides Function DoUpdateCheck() As Boolean
        MyBase.DoUpdateCheck()
        DoUpdateCheck = False
        Me.ValidateChildren()

        If Me.cboSoftWareType.Text.Trim = String.Empty Then
            ShowMessageBox("支出预算类型不能为空")
            Me.btnSelectType.Focus()
            Exit Function
        End If

        If numUpDown.Value <= 0 Then
            ShowMessageBox("支出预算不能小于0")
            Me.numUpDown.Focus()
            Exit Function
        End If

        If Me.txtUsername.Text.Trim = String.Empty Then
            ShowMessageBox("支出预算用户不能为空")
            Me.btnUsername.Focus()
            Exit Function
        End If

        DoUpdateCheck = True
    End Function

    Public Overrides Function DoUpdateExist() As Boolean
        DoUpdateExist = False
        MyBase.DoUpdateExist()
        If CStr(Me.参数列表("ID")).Trim = "" Then
            ShowMessageBox("没有选择任何记录")
            Exit Function
        End If
        DoUpdateExist = True
    End Function

    Public Overrides Sub InitUpdate()
        MyBase.InitUpdate()
        Me.Text = "支出预算更新"

        Dim rs As ADODB.Recordset = Nothing
        Dim sb As New StringBuilder
        sb.AppendLine(" select budgetOut.*,WysInputUser.UserName as UserName1")
        sb.AppendLine("  from budgetOut ")
        sb.AppendLine("left join WysInputUser on WysInputUser.id =budgetOut.username  WHERE budgetOut.id='" + CStr(Me.参数列表("ID")) + "'")
        rs = ct.GetTable(sb.ToString)

        If rs IsNot Nothing Then
            If rs.EOF = False Then
                Me.dt1.Value = New Date(CInt(rs.Fields("year").Value), CInt(rs.Fields("month").Value), 1)
                Me.txtMemo.Text = CMainModule.CmnToStr(rs.Fields("Memo").Value)
                Me.numUpDown.Text = CDec(rs.Fields("Count").Value)
                Me.cboSoftWareType.Tag = UTIL.CUtil.ConvertText(rs.Fields("type").Value)
                Me.cboSoftWareType.Text = ct.GetParentName(Me.cboSoftWareType.Tag, "XscBugType", "Name", "ParentID", "ID")
                Me.txtUsername.Text = UTIL.CUtil.ConvertText(rs.Fields("UserName1").Value)
                Me.txtUsername.Tag = UTIL.CUtil.ConvertText(rs.Fields("UserName").Value)
            End If
            rs.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs)
        End If
        rs = Nothing
    End Sub

#End Region

    Private Sub btnUsername_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUsername.Click
        Using frm As Wys.Control.Vb.ListForm = New Wys.Control.Vb.ListForm
            frm.Text = "支出预算人一览"
            frm.strName = Me.strName
            frm.iType = Me.iType
            frm.SQL = "select ID,username as 名称,memo as 备注,UserIndex from WysInputUser  where type=0 order by UserIndex"
            Dim 显示列 As New StringCollection
            显示列.Add("名称")
            显示列.Add("备注")
            frm.Width = 250
            frm.Height = 200
            frm.显示列 = 显示列
            frm.ShowDialog()
            If frm.返回值.Count > 0 Then
                txtUsername.Tag = CStr(frm.返回值(0))
                txtUsername.Text = CStr(frm.返回值(1))
            End If
        End Using
    End Sub

End Class